package com.demo.money.kit;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * SupplementUtils类<br>
 * Date相关操作
 * <p>
 * <b>常用方法</b><br>
 * getFristDateOfMonth - 本月第一天(格式：yyyy-MM-dd)<br>
 * getLastDateOfMonth - 本月最后一天(格式：yyyy-MM-dd)<br>
 * getMondayOfThisWeek - 本周周一(格式：yyyy-MM-dd)<br>
 * getSundayOfThisWeek - 本周周日(格式：yyyy-MM-dd)<br>
 * supplementByDay - 单个数据按日期补零(格式：yyyy-MM-dd)
 * <p>
 * <b>私有方法</b><br>
 * compare - 获取两个日期相隔的天数
 * <p>
 * <b>全类名</b>:com.example.demo1.kit<br>
 * <b>类名</b>:SupplementUtils<br>
 * <b>创建者</b>:张峰浦<br>
 * <b>创建时间</b>:2020/3/12 21:02<br>
 */
public class SupplementUtils {
    /**
     * 获取两个日期相隔的天数
     *
     * @param startTime 起始时间
     * @param endTime 结束时间
     * @return 间隔天数
     */
    private static Integer compare(Date startTime, Date endTime) {

        Long num1 = startTime.getTime();

        Long num2 = endTime.getTime();

        return (int) ((num2 - num1) / (24 * 3600 * 1000));

    }

    /**
     * 得到本月第一天
     *
     * @return yyyy-MM-dd
     */
    public static String getFristDateOfMonth() {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date dt = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(dt);

        int days = cal.getActualMinimum(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, days);

        return format.format(cal.getTime());

    }

    /**
     * 得到本月最后一天
     *
     * @return yyyy-MM-dd
     */
    public static String getLastDateOfMonth() {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date date = new Date();

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        cal.set(Calendar.DAY_OF_MONTH, days);

        return format.format(cal.getTime());

    }

    /**
     * 得到本周周一
     *
     * @return yyyy-MM-dd
     */
    public static String getMondayOfThisWeek() {

        Calendar c = Calendar.getInstance();

        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;

        if (dayofweek == 0) {

            dayofweek = 7;

        }

        c.add(Calendar.DATE, -dayofweek + 1);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(c.getTime());

    }

    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static String getSundayOfThisWeek() {

        Calendar c = Calendar.getInstance();

        int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;

        if (dayofweek == 0) {

            dayofweek = 7;

        }

        c.add(Calendar.DATE, -dayofweek + 7);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(c.getTime());

    }

    /**
     * 单个数据按日期补零(格式：yyyy-MM-dd)
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return yyyy-MM-dd
     */
    public static List<String> supplementByDay(String startTime, String endTime) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date start, end;

        try {

            start = simpleDateFormat.parse(startTime);

            end = simpleDateFormat.parse(endTime);

        } catch (Exception e) {

            return null;

        }

        Integer days = compare(start, end);

        List<String> reusltList = new ArrayList<>();

        for (int i = 0; i < days + 1; i++) {

            Calendar cal = Calendar.getInstance();

            cal.setTime(start);

            cal.add(Calendar.DAY_OF_MONTH, 1);

            String time = simpleDateFormat.format(start);

            reusltList.add(time);

            start = cal.getTime();

        }

        return reusltList;

    }

}
